package com.jumper.property.comparer.core.diff.value;


import com.jumper.property.comparer.core.bean.PropertySetting;
import com.jumper.property.comparer.core.diff.Diff;
import com.jumper.property.comparer.core.diff.DiffVal;
import com.jumper.property.comparer.core.formatter.ObjectDiffFormatter;

import java.util.ArrayList;
import java.util.List;

public class ObjectDiff implements Diff {

    private final PropertySetting setting;
    private final Object beforeValue;
    private final Object afterValue;
    private final List<DiffVal> subDiffs;

    public ObjectDiff(PropertySetting setting, Object beforeValue, Object afterValue, List<DiffVal> subDiffs) {
        this.setting = setting;
        this.beforeValue = beforeValue;
        this.afterValue = afterValue;
        this.subDiffs = subDiffs;
    }

    @Override
    public List<DiffVal> getDiffValues() {
        List<DiffVal> diffValList = new ArrayList<>();
        if (subDiffs.size() > 0) {
            diffValList.add(new DiffVal(
                    beforeValue, afterValue, getDiffValue()
            ));
        }
        return diffValList;
    }

    private String getDiffValue() {
        return new ObjectDiffFormatter(setting.getPropertyName(), subDiffs).format();
    }
}
